package com.car.account.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.account.entity.PlatformRole;
import com.car.account.service.PlatformRoleService;
import com.car.commons.core.model.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 董旭
 * @since 2024-08-28
 */
@RestController
@RequestMapping("/adminRole")
public class AdminRoleController {

    @Resource
    private PlatformRoleService platformRoleService;

    // 新增或者更新
    @PostMapping("/role.pt")
    public Result save(@RequestBody PlatformRole role) {
        platformRoleService.saveOrUpdate(role);
        return Result.getResult();
    }

    @GetMapping("/page.pt")
    public Result findPage(@RequestParam String name,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<PlatformRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("role_name", name);
        queryWrapper.orderByDesc("role_id");
        return Result.getResult().setData(platformRoleService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    /**
     * 绑定角色和菜单的关系
     * @param roleId 角色id
     * @param menuIds 菜单id数组
     * @return
     */
    @PostMapping("/roleMenu/{roleId}.pt")
    public Result roleMenu(@PathVariable Integer roleId, @RequestBody List<Integer> menuIds) throws Exception {
        platformRoleService.setRoleMenu(roleId, menuIds);
        return Result.getResult();
    }

    @GetMapping("/roleMenu/{roleId}.pt")
    public Result getRoleMenu(@PathVariable Integer roleId) throws Exception {
        return Result.getResult().setData( platformRoleService.getRoleMenu(roleId));
    }

}

